/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view.hyperlink;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import offset.nodes.client.model.PanelContainer;

/**
 *
 * @author  Walter Lütgenau
 */
public class ExternalHyperlinkPanel extends javax.swing.JPanel {

    PanelContainer container;

    /** Creates new form DataHyperlinkPanel */
    public ExternalHyperlinkPanel() {
        initComponents();
    }

    class TextFieldDocumentListener implements DocumentListener {

        public void changedUpdate(DocumentEvent e) {
            validateOkButton();
        }

        public void insertUpdate(DocumentEvent e) {
            validateOkButton();
        }

        public void removeUpdate(DocumentEvent e) {
            validateOkButton();
        }
    }

    protected void validateOkButton() {
        if (textTextfield.getText() != null && textTextfield.getText().length() > 0 &&
                urlTextfield.getText() != null && urlTextfield.getText().length() > 0)
            container.enableOkButton(true);
        else
            container.enableOkButton(false);
    }

    public void setText(String text) {
        textTextfield.setText(text);
    }
    
    public void init(PanelContainer container) {
        this.container = container;
        
        textTextfield.getDocument().addDocumentListener(new TextFieldDocumentListener());
        urlTextfield.getDocument().addDocumentListener(new TextFieldDocumentListener());
        
        validateOkButton();
    }

    public ExternalHyperlinkInfo getResult() {
        return new ExternalHyperlinkInfo(urlTextfield.getText(), textTextfield.getText());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textLabel = new javax.swing.JLabel();
        urlLabel = new javax.swing.JLabel();
        textTextfield = new javax.swing.JTextField();
        urlTextfield = new javax.swing.JTextField();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        textLabel.setText(bundle.getString("Hyperlink.Page.text")); // NOI18N

        urlLabel.setText(bundle.getString("Hyperlink.External.url")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(urlLabel)
                        .addGap(44, 44, 44)
                        .addComponent(urlTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textLabel)
                        .addGap(41, 41, 41)
                        .addComponent(textTextfield, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)))
                .addGap(59, 59, 59))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urlLabel)
                    .addComponent(urlTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textLabel)
                    .addComponent(textTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(136, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel textLabel;
    private javax.swing.JTextField textTextfield;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JTextField urlTextfield;
    // End of variables declaration//GEN-END:variables
}
